<?php
class My_View_Helper_MenuHelper extends Zend_View_Helper_Abstract
{
    protected $_categories;
    protected $_rootHref = '';
    protected $_categoryId;
    protected $_departmentRootHref;

    public function __construct() {                
        $this->_categories = new Application_Model_Categories();
    }    

    public function menuHelper($departmentRoot='', $categoryId=0) {
        $this->_categoryId         = $categoryId;
        $this->_departmentRootHref = $departmentRoot;        
        $this->_buildMenu();
    }

    protected function _routeRules($type, $alias) {        
        $rules = array(
            'title'      => '',
            'static'     => $this->_departmentRootHref.'/static/' . $alias,
            'department' => $this->_rootHref.'/department/' . $alias,
            'feed'       => $this->_departmentRootHref.'/feed/'.$alias,      
            'custom'     => $this->_departmentRootHref.'/custom/'.$alias
        );
        return $rules[$type];
    }

    protected function _buildMenu() {
        //if we build menu for depts - don't consider
        //category visible(in menu) settings       
        $isDeptCategories = !$this->_categoryId ? false : true;        
        if ($isDeptCategories) {
            $cacheName = $this->_categoryId;
            $tags = array('dept_menu');
        } else {
            $cacheName = 'menu';
            $tags = array();
        }   
        $cacheName = $isDeptCategories ? $this->_categoryId : 'menu';             
        //My_Cache::clearCache($cacheName);
        if ($menu = My_Cache::getCache($cacheName)) {
            echo $menu;
            return;
        }        
        $allParents = $this->_categories->getCategoriesByParentId($this->_categoryId, $isDeptCategories);        
        
        $menu = '';
        foreach ($allParents as $parent) {            
            $menu .= $this->_buildNode($parent);            
        }        
        //die();
        My_Cache::saveCache($menu, $cacheName);
        var_dump($menu);
        die(1);
        echo $menu;        
    }

    protected function _getChildNodes($parentId, $rootHref) {        
        $children = $this->_categories->getCategoriesByParentId($parentId);                
        $childNode = '';
        if ($children) {                    
            $childNode .= '<ul>';
            foreach ($children as $child) {
                $childNode .= $this->_buildNode($child, $rootHref);                
            }
            $childNode .= '</ul>';
        }
        return $childNode;
    }

    protected function _buildNode(array $parent, $rootHref='') {
        $href  = $rootHref . $this->_routeRules($parent['type'], $parent['alias']); 
        $node  = '<li>';
        $node .= '<a href="'.$href.'"';
        $node .= 'title="'.$parent['category_name'].'">';
        $node .= $parent['category_name'];
        $node .= '</a>';        
        $href  = $rootHref?$rootHref:$href;        
        $node .= $this->_getChildNodes($parent['id'], $href);
        $node .= '</li>';            
        return $node;
    }    
}